//
//  ViewController.swift
//  SQPageView
//
//  Created by ToothBond on 2017/4/24.
//  Copyright © 2017年 rensq. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    
    fileprivate let kCellLabelTag : Int = 100

    override func viewDidLoad() {
        super.viewDidLoad()
        
        automaticallyAdjustsScrollViewInsets = false
        
        //pageViews with ViewController
        //setupPageControllers()
        
        //pageView with custom view
        setupPageViews()
    }


}

extension ViewController {
    fileprivate func setupPageControllers() {
        let pageFrame = CGRect(x: 0, y: 64, width: view.bounds.width, height: view.bounds.height - 64)
        let titles = ["精选","电影","电视剧","腾讯VIP年度会员","游戏","娱乐","综艺-跑来跑去","纪录片", "美剧"];
        
        var childVCs = [UIViewController]()
        for _ in 0..<titles.count {
            let vc = UIViewController()
            vc.view.backgroundColor = UIColor.randomColor()
            childVCs.append(vc)
        }
        
        var pageStyle = SQPageStyle()
        pageStyle.isScrollEnable = true
        pageStyle.isShowBottomLine = false
        pageStyle.isShowCoverView = true
        
        let pageView = SQPageView(frame: pageFrame, titles: titles, style: pageStyle, childVCs: childVCs, parentVC: self)
        
        view.addSubview(pageView)
    }
    
    
    
    fileprivate func setupPageViews() {
        let pageFrame = CGRect(x: 0, y: 200, width: view.bounds.width, height: 300)
        let titles = ["推荐","游戏","娱乐","趣玩"]
        var style = SQPageStyle()
        style.isShowBottomLine = true
        
        let layout = SQPageCollectionLayout()
        layout.sectionInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
        layout.itemMargin = 5
        layout.lineMargin = 5
        layout.numberOfCol = 4
        layout.numberOfRow = 2
        
        let pageCollectionView = SQPageCollectionView(frame: pageFrame, titles: titles, style: style, layout: layout)
        pageCollectionView.dataSource = self
        pageCollectionView.registerClass(UICollectionViewCell.self, forCellReuseableIdentifier: "UICollectionViewCell")
        view.addSubview(pageCollectionView)
    }
}

extension ViewController : SQPageCollectionDataSource {
    func numberOfSectionInPageCollectionView(_ pageCollectionView: SQPageCollectionView) -> Int {
        return 4
    }
    
    func pageCollectionView(_ pageCollectionView: SQPageCollectionView, numberOfItemsInSection section: Int) -> Int {
        if section == 3 {
            return 7
        }
        return 12
    }
    
    func pageCollectionView(_ pageCollectionView: SQPageCollectionView, _ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "UICollectionViewCell", for: indexPath)
        cell.backgroundColor = UIColor.randomColor()
        return cell
    }
}

